﻿#nullable disable
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using Blogs.LazyFox.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Authorization;

using Blogs.LazyFox.Data;

namespace Blogs.LazyFox.Controllers;
[AllowAnonymous]
public class HomeController : Controller
{
    private readonly ILogger<HomeController> _logger;

    private readonly BlogsContext _blogsContext;

    public HomeController(ILogger<HomeController> logger, BlogsContext blogsContext)
    {
        _logger = logger;
        _blogsContext = blogsContext;
    }

    public async Task<IActionResult> Index()
    {
        var blogs = from b in _blogsContext.Blogs
                    select b;

        return View(await blogs.OrderByDescending(b => b.CreateTime).AsNoTracking().ToListAsync());
    }

    public IActionResult Privacy()
    {
        return View();
    }

    [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
    public IActionResult Error()
    {
        return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
    }
}
